﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Faust.Andrew.MTG.Player
{
	/// <summary>
	/// Interaction logic for TargetPlayerDialog.xaml
	/// </summary>
	public partial class TargetPlayerDialog : Window
	{        
        static DependencyProperty PresenterProperty = DependencyProperty.Register("Presenter", typeof(GamePresenter), typeof(TargetPlayerDialog));         
        public GamePresenter Presenter
        {
            get
            {
                return (GamePresenter)base.GetValue(PresenterProperty);
            }
            set
            {
                base.SetValue(PresenterProperty, value);
            }
        }

        static DependencyProperty RandomCardsProperty = DependencyProperty.Register("RandomCards", typeof(bool), typeof(TargetPlayerDialog));         
        public bool RandomCards
        {
            get
            {
                return (bool)base.GetValue(RandomCardsProperty);
            }
            set
            {
                base.SetValue(RandomCardsProperty, value);
            }
        }

        static DependencyProperty SelectTargetsProperty = DependencyProperty.Register("SelectTargets", typeof(bool), typeof(TargetPlayerDialog));
        public bool SelectTargets
        {
            get
            {
                return (bool)base.GetValue(SelectTargetsProperty);
            }
            set
            {
                base.SetValue(SelectTargetsProperty, value);
            }
        }

        private int _targetPlayerId;
        public int TargetPlayerId
        {
            get { return _targetPlayerId; }
            set
            {
                _targetPlayerId = value;
            }
        }

        public Shared.Zone SourceZone { get; private set; }

        public int Count { get; private set; }

        public System.Collections.Generic.List<int> TargetPlayers { get; private set; }

        public TargetPlayerDialog()
		{
			this.InitializeComponent();
			
			// Insert code required on object creation below this point.
            cbZone.ItemsSource = Enum.GetNames(typeof(Shared.Zone));
		}        



		private void btnOK_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            TargetPlayers = new List<int>();
            
            if (SelectTargets)
            {
                PlayerState player = lbTarget.SelectedItem as PlayerState;
                if (player == null)
                {
                    MessageBox.Show("You must select a target or cancel");
                    return;
                }

                _targetPlayerId = player.PlayerID;

                foreach (object tempPlayer in lbTarget.SelectedItems)
                {
                    if (tempPlayer is PlayerState)
                    {
                        TargetPlayers.Add((tempPlayer as PlayerState).PlayerID);
                    }
                }
            }

            if (RandomCards)
            {
                if (cbZone.SelectedItem == null)
                {
                    MessageBox.Show("You must select a source zone");
                    return;
                }

                SourceZone = (Shared.Zone)Enum.Parse(typeof(Shared.Zone), cbZone.Text);

                int count;
                if (!Int32.TryParse(txtCount.Text, out count))
                {
                    MessageBox.Show("You must enter a card count");
                    return;
                }

                if (count < 1)
                {
                    MessageBox.Show("You must enter a card count > 0");
                    return;
                }

                Count = count;
            }

            DialogResult = true;
            Hide();
		}

		private void btnCancel_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            _targetPlayerId = -1;
            TargetPlayers = new List<int>();
            DialogResult = false;
            Hide();
		}
	}
}